import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    todos: [
      {
        id: 1,
        title: '得得得得得',
        completed: false
      },
      {
        id: 2,
        title: '哒哒哒哒哒',
        completed: false
      },
      {
        id: 3,
        title: '滴滴滴滴滴',
        completed: false
      }
    ]
  },
  mutations: {
    addItemVuex(state, text) {
      state.todos.push({
        id: Math.random(),
        title: text,
        completed: false
      })
    },
    changeItemVuex(state, id) {
      const tmp = [...state.todos]
      state.todos = tmp.map(todo => {
        if (todo.id === id) {
          todo.completed = !todo.completed
        }
        return todo
      })
    },
    delItemVuex(state, id) {
      state.todos = state.todos.filter(todo => {
        return todo.id !== id
      })
    }
  }
})

export default store
